package com.maiworld.seller.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.maiworld.mapper.TbConsumeMapper;
import com.maiworld.mapper.TbStudentMapper;
import com.maiworld.pojo.TbConsume;
import com.maiworld.pojo.TbConsumeExample;
import com.maiworld.pojo.TbConsumeExample.Criteria;
import com.maiworld.pojo.TbStudent;
import com.maiworld.seller.service.ConsumeService;

import entity.PageResult;

@Service
public class ConsumeServiceImpl implements ConsumeService {

	@Autowired
	private TbConsumeMapper tbConsumeMapper;
	
	@Autowired
	private TbStudentMapper tbStudentMapper;
	
	//添加或加少金币
	@Override
	public Map<String, Object> updateStudent(Long sid,String flag,String coinsNum) {
		
		Map<String, Object> result = new HashMap<String, Object>();
		
		TbStudent stu = new TbStudent();
		stu = tbStudentMapper.selectByPrimaryKey(sid);
		int b = stu.getPoints();
		//System.out.print(b);
		int a =Integer.parseInt(coinsNum);
		if(flag.equals("0")){
			b = b - a;
			
		}else if(flag.equals("1")){
			b = b + a;
		}

		stu.setPoints(b);
		tbStudentMapper.updateByPrimaryKeySelective(stu);
		result.put("statusCode", 200);			
		result.put("message", "增加"+a+"金币");
		result.put("message", "减少"+a+"金币");
		result.put("points", b);
		
		return result;
	}

	public Map<String, Object> findOne(String status, String typelog){
		
		HashMap<String, Object> result = new HashMap<String,Object>();
		
		TbConsume a = new TbConsume();
		a.setStatus(status);
		a.setTypelog(typelog);
		
		List<TbConsume> c = tbConsumeMapper.selectByConsume(a);
		
		result.put("flag", c.get(0).getFlag());
		result.put("number_coins", c.get(0).getNumberCoins());
		result.put("statusCode",200 );
		result.put("message", "获取成功");
		return result;
		
	}

	@Override
	public List<TbConsume> findAll() {
		// TODO Auto-generated method stub
		return tbConsumeMapper.selectByExample(null);
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize);
		Page<TbConsume> page = (Page<TbConsume>) tbConsumeMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult()) ;
	}

	@Override
	public void add(TbConsume consume) {
		// TODO Auto-generated method stub
		tbConsumeMapper.insert(consume);
	}

	@Override
	public void update(TbConsume consume) {
		// TODO Auto-generated method stub
		tbConsumeMapper.updateByPrimaryKey(consume);
	}

	@Override
	public TbConsume findOne(Long id) {
		// TODO Auto-generated method stub
		return tbConsumeMapper.selectByPrimaryKey(id);
	}

	@Override
	public void delete(Long[] ids) {
		// TODO Auto-generated method stub
		for(Long id:ids){
			tbConsumeMapper.deleteByPrimaryKey(id);
		}
	}

	@Override
	public PageResult findPageConsume(TbConsume consume, int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize);
		TbConsumeExample example = new TbConsumeExample();
		Criteria create = example.createCriteria();
		if(consume!=null){
			if(consume.getStatus()!=null && consume.getStatus().length()>0){
				create.andStatusEqualTo(consume.getStatus());
			}
		}
		Page<TbConsume> page = (Page<TbConsume>) tbConsumeMapper.selectByExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}
	
	
	

}
